var network = require('/api/api.js')
App({
  onLaunch: function() {
    this.getOpenid()
  },
  getOpenid: function() {
    let that = this;
    wx.login({
      success: res => {
        console.log(res)
        if (res.code) {
          wx.request({
            url: network.Connect.baseURL +'weixin/login',
            data: {
              code: res.code
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            success: res => {
              console.log('openid',res)
              that.globalDataO.openid = res.data
              var sessionKey = res.data.sessionKey
              that.getUser()
              //储存返回的openid
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
          reject('error');
        }
      }
    })
  },
  // 获取用户信息
  getUser: function() {
    var that = this
    return new Promise(function(resolve, reject) {
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                resolve(res)
                console.log(res)
                if (res.userInfo) {
                  that.globalData.userInfo = res.userInfo
                  that.globalData.userInfo['encryptedData'] = res.encryptedData
                  that.globalData.userInfo['iv'] = res.iv
                  that.getUid()
                } else {
                  wx.showToast({
                    title: '网络出错',
                  })
                }
                console.log(that.globalData.userInfo)
                if (that.userInfoReadyCallback) {
                  that.userInfoReadyCallback(res)
                }
              },
            })
          }else{
            that.login.login=1
            console.log('未授权',that.login.types)
          }
        }
      })
    })
  },
  // //接口
  // getUserOpenid: function(cb) {
  //   var that=this
  //   if (that.globalDataO.openid) {
  //     typeof cb == 'function' && cb(that.globalDataO.openid)
  //   } else {
  //     wx.login({
  //       success: function(res) {
  //         var code = res.code
  //         wx.request({
  //           method: 'POST',
  //           header: {
  //             "Content-Type": "application/x-www-form-urlencoded"
  //           },
  //           url: 'http://192.168.2.120:8080/weixin/login ',
  //           data: {
  //             code: code,
  //             grant_type: 'authorization_code'
  //           },
  //           success: function(res) {
  //             console.log(res)
  //             var data = res.data
  //             if (data) {
  //               var sessionKey = data.sessionKey;
  //               that.getUid(sessionKey)
  //               that.globalDataO.openid = data
  //               console.log(that.globalDataO.openid)
  //             }
  //           },
  //           fail: function(e) {
  //             wx.showToast({
  //               title: '网络异常！',
  //               duration: 2000,
  //             })
  //           },
  //         })
  //       },
  //     })
  //   }
  // },
  // //获取uniid
  getUid() {
    let that = this
    return new Promise(function(resolve, reject) {
      wx.request({
        url: network.Connect.baseURL+'weixin/login/userinfo',
        method: 'GET',
        header: {
          'content-type': 'application/json',
        },
        data: {
          encryptedData: that.globalData.userInfo.encryptedData,
          sessionKey: that.globalDataO.openid.sessionKey,
          iv: that.globalData.userInfo.iv,
          userId: that.user.user
        },
        success(res) {
          console.log(res.data.objectSid)
          that.globalDataO.openid['objectSid'] = res.data.objectSid;
          console.log(that.globalDataO.openid.objectSid)
          that.login.types =0
          console.log('yonghu ', that.login.types)
        }
      })
    })
  },
  //封装请求函数  post
  post: function(url, data) {
    var promise = new Promise((resolve, reject) => {
      var that = this;
      //网络请求      
      wx.request({
        url: url,
        data: data,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          resolve(res);
        },
        fail: function(e) {
          reject('网络出错');
        }
      })
    });
    return promise;
  },
  //封装请求函数  post
  postJson: function(url, data) {
    var promise = new Promise((resolve, reject) => {
      var that = this;
      //网络请求      
      wx.request({
        url: url,
        data: data,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function(res) {
          resolve(res);
        },
        fail: function(e) {
          reject('网络出错');
        }
      })
    });
    return promise;
  },
  //封装请求函数  get
  gets: function(url) {
    var promise = new Promise((resolve, reject) => {
      var that = this;
      //网络请求      
      wx.request({
        url: url,
        data: {},
        method: 'GET',
        header: {
          'content-type': 'application/json',
        },
        success: function(res) {
          resolve(res);
        },
        fail: function(e) {
          reject('网络出错');
        }
      })
    });
    return promise;
  },
  //封装请求函数  get
  getJson: function(url, data) {
    var promise = new Promise((resolve, reject) => {
      var that = this;
      //网络请求      
      wx.request({
        url: url,
        data: data,
        method: 'GET',
        header: {
          'content-type': 'application/json',
        },
        success: function(res) {
          resolve(res);
        },
        fail: function(e) {
          reject('网络出错');
        }
      })
    });
    return promise;
  },

  //接口
  url: {
    ceshi: network.Connect.baseURL + 'wechat/desk/myIndex',
    index: network.Connect.baseURL + 'wechat/restaurant/list',
    //餐厅分类
    authorlist: network.Connect.baseURL + 'wechat/restaurant/authorlist',
    //获取餐厅数据
    restaurant: network.Connect.baseURL + 'wechat/restaurant/list',
    // 会员卡
    member: network.Connect.baseURL + 'wechat/desk/member',
    //收货地址
    address: {
      addAddress: network.Connect.baseURL + 'system/memAddress/addMemAddress',
      getSelfAddress: network.Connect.baseURL + 'system/memAddress/userList',
      getOneAddress: network.Connect.baseURL + 'system/memAddress/addressId', //获取单个收货地址
      editAddress: network.Connect.baseURL + 'system/memAddress/updateMemAddress', //修改收货地址
      deleteAddress: network.Connect.baseURL + 'system/memAddress/updateIsActived', //删除收货地址
    },
    // 公告
    notice: network.Connect.baseURL + 'wechat/desk/blogContent',
    //餐厅信息
    getRestaurantInfo: network.Connect.baseURL + 'wechat/restaurant/getRestaurantInfo',
    //餐品分类
    desk: network.Connect.baseURL + 'wechat/desk/index',
    //创建订单
    DCopenTable: network.Connect.baseURL + 'wechat/desk/DCopenTable',
    //外卖选餐
    indexTwo :network.Connect.baseURL +'wechat/desk/indexTwo',
    //抽奖券列表
    userLuckList: network.Connect.baseURL + '/system/memLuck/userLuckList',
    //选择抽奖券
    getMemberLuck: network.Connect.baseURL+'wechat/desk/getMemberLuck',
    //获取餐桌信息
    getTableList: network.Connect.baseURL + 'wechat/desk/getTableList',
    // 发送验证码
    isCode: network.Connect.baseURL + 'hall/sendSms',
    // 绑定手机号
    bindPhone: network.Connect.baseURL + 'wechat/desk/bindingPhone',
    // 查询绑定手机号码
    selectPhone: network.Connect.baseURL + 'wechat/desk/getMemberByMemId',
    // 推广记录
    spread: network.Connect.baseURL + 'system/ofRecord/promotionRecord',
    //获取餐桌信息和地址信息
    userDefulAddress: network.Connect.baseURL + 'wechat/desk/userDefulAddress',
    //菜品分类
    categoryList: network.Connect.baseURL +'wechat/desk/categoryList',
    //订单选餐
    updateBillLine: network.Connect.baseURL+'wechat/desk/updateBillLine',
    //购物车详情
    billLine: network.Connect.baseURL + '/wechat/desk/billLine',
    //清空购物车
    deletesbillLine: network.Connect.baseURL +'wechat/desk/cleanBillLine',
    seatprice: network.Connect.baseURL +'wechat/desk/seatprice',
    // 抽奖
    //计算物流费、
    logfeeprice: network.Connect.baseURL +'wechat/desk/logfeeprice',
    //后台计算的奖项
    Lottery: network.Connect.baseURL +'system/luck/Lottery',
    lottery: {
      voucher: network.Connect.baseURL + 'system/luck/luckList',//获得奖项
      degree: network.Connect.baseURL + 'system/memLuck/userLuckCount', //获得抽奖次数
      award: network.Connect.baseURL +'system/memLuck/userLuck', //会员抽奖
    },
    //会员支付修改价格
    updateprice: network.Connect.baseURL+'wechat/desk/updateprice',
    //会员卡支付
    memberCardPay: network.Connect.baseURL + 'memberCardPay',
    //微信支付
    wxPay: network.Connect.baseURL +'weixin/wxPay',
    wxPay1: network.Connect.baseURL + 'weiChat/wxPay',
    //支付回调
    wxNotify: network.Connect.baseURL +'weixin/wxNotify',
    //查询餐厅评论
    secComment: network.Connect.baseURL +'/system/secComment/listtwo',
    //增加评论
    secCommentsave: network.Connect.baseURL +'/system/secComment/saveAndUpdate',
    //上傳圖片
    saveimg: network.Connect.baseURL +'system/secComment/images',
  //userid
    userid: network.Connect.baseURL +'weixin/getUserId',
    // 订单
    order: {
      select: network.Connect.baseURL + 'wechat/desk/billHisList', //查询订单
      delete: network.Connect.baseURL + 'wechat/desk/delBillHisList',//删除订单
      detail: network.Connect.baseURL + 'wechat/desk/billHis', // 查询订单详情,
      orderpass: network.Connect.baseURL +'cas/order/orderpass',//取消订单
      cash: network.Connect.baseURL +'wechat/desk/getBill',//查询历史订单
    }
  },
  globalData: {
    userInfo: null
  },
  globalDataO: {
    openid: null
  },
  user:{
    user:''
  },
  login:{
    types:''
  },
  globalDataU: {
    unionid: null
  },
})